<?php

/**
 * 对图书的评论
 */
class CommentAR extends CActiveRecord {
	public static function model($className=__CLASS__){
		return parent::model($className);
	}

	public function tableName(){
		return self::tblName();
	}
	
	public static function tblName(){
		return 'ccs8_comment';
	}
	
	public static function saveComment($bookId,$commentContent){
		$ar = new CommentAR;
		$ar->fk_book=$bookId;
		$ar->fk_user=User::id();
		$ar->user_name=User::name();
		$ar->say_ip=MyUtil::getRemoteAddr();
		$ar->say_text=$commentContent;
		$ar->say_time=date('Y-m-d H:i:s');
		$ar->save();
		return $ar->comment_id;
	}

    public static function topNewComments($limit = 10){
        $sql = 'select fk_book,user_name,say_text ';
        $sql .= DBUtil::buildFromSingleTbl(self::tblName());
        $sql .= 'order by say_time desc limit '.$limit;
        return DBUtil::queryAll($sql);
    }

    public static function commentsOfBook($bookId = 0){
        $sql = 'select user_name,say_text,say_time ';
        $sql .= DBUtil::buildFromSingleTbl(self::tblName());
        $sql .= 'where fk_book = '.$bookId;
        $sql .= ' order by comment_id';
        return DBUtil::queryAll($sql);
    }
    
    public static function delByBooId($bookId = 0){
    	$sql = 'delete from '.self::tblName().' where fk_book = '.$bookId;
    	DBUtil::exec($sql);
    }
}
